<?php
namespace wogu2\http\header;

class SetCookie implements MultipleHeaderInterface
{
    protected $_name = null;
    protected $_value = null;
    protected $_version = null;
    protected $_maxAge = null;
    protected $_expires = null;
    protected $_domain = null;
    protected $_path = null;
    protected $_secure = null;
    protected $_httponly = null;
    
    public function __construct($name, $value, $expires = null, $path = null, $domain = null, $secure = false, $httponly = false, $maxAge = null, $version = null) {
        $this->_name = $name;
        $this->_value = $value;
        $this->_version = $version;
        $this->_maxAge = $maxAge;
        $this->_expires = $expires;
        $this->_domain = $domain;
        $this->_path = $path;
        $this->_secure = $secure;
        $this->_httponly = $httponly;
    }
    
    function toString() {
        $value = urlencode($this->_value);
        
        $fieldValue = $this->_name . '=' . $value;
        
        if($this->_version !== null) {
            $fieldValue .= '; Version=' . $this->_version;
        }

        if ($this->_maxAge !== null) {
            $fieldValue .= '; Max-Age=' . $this->_maxAge;
        }

        if ($this->_expires) {
            $fieldValue .= '; Expires=' . $this->getExpires();
        }

        if ($this->_domain) {
            $fieldValue .= '; Domain=' . $this->_domain;
        }

        if ($this->_path) {
            $fieldValue .= '; Path=' . $this->_path;
        }

        if ($this->_secure) {
            $fieldValue .= '; Secure';
        }

        if ($this->_httponly) {
            $fieldValue .= '; HttpOnly';
        }

        return 'Set-Cookie: ' . $fieldValue;
    }
    
    public function getExpires() {
        return gmdate('D, d-M-Y H:i:s', $this->_expires) . ' GMT';
    }
}